<script lang="ts" setup name="XtxCity">
import { ref, watch } from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'

export type ChangeResult = {
  provinceCode: string
  provinceName: string
  cityCode: string
  cityName: string
  countyCode: string
  countyName: string
}

defineProps<{
  userAddress?: string
}>()

// const emit = defineEmits(['changeCity'])
const emit = defineEmits<{
  (e: 'changeCity', value: ChangeResult): void
}>()

// 省市区是树形数据
type AreaList = {
  name: string
  code: string
  level: number
  areaList: AreaList[]
}

// 记录弹层的显示隐藏
const active = ref(false)

// 需求: 点击了外部区域, 也需要关闭弹层 => 需要知道点了外面 => onClickOutside()
const target = ref(null)

// onClickOutside(目标元素, 回调)  => 回调会在   监听到目标元素之外的点击时 执行
onClickOutside(target, () => {
  active.value = false
})

// 注意点:
// 1.请求的是阿里云服务器, 不能用request请求 直接axios
// 2.省市区数据不需要存在pinia, 放在组件内部即可

const cityList = ref<AreaList[]>([]) // 用于渲染选择区域
const cacheList = ref<AreaList[]>([]) // 用于缓存城市列表信息

async function getCityList() {
  // 获取城市数据
  const res = await axios.get<AreaList[]>(
    'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
  )
  // console.log(res.data[0].)
  cityList.value = res.data
  cacheList.value = res.data
}

getCityList()

// 记录选择的省市区的名字以及code
const changeResult = ref({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: '',
})

// 1.能够一级一级往下选 2.记录了用户的选择
const selectCity = (item: AreaList) => {
  console.log(item)

  if (item.level === 0) {
    // 选择了省
    changeResult.value.provinceCode = item.code
    changeResult.value.provinceName = item.name
    cityList.value = item.areaList
  }

  if (item.level === 1) {
    // 选择了市
    changeResult.value.cityCode = item.code
    changeResult.value.cityName = item.name
    cityList.value = item.areaList
  }

  if (item.level === 2) {
    // 选择了区
    changeResult.value.countyCode = item.code
    changeResult.value.countyName = item.name

    active.value = false // 关闭弹层
    // 重置 cityList 为 省的信息  => 希望只要用户关了弹层, 无论是在选择省/市/区, 都应该重置

    // 子传父 => this.$emit  /  emit
    emit('changeCity', changeResult.value)
  }
}

// 只要弹层关了, 无论用户在选啥, 就应该重置
watch(active, (value) => {
  // console.log(value)
  if (!value) {
    cityList.value = cacheList.value
  }
})
</script>
<template>
  <div class="xtx-city" ref="target">
    <!-- 展示区域 -->
    <div @click="active = !active" class="select" :class="{ active }">
      <span class="value" v-if="userAddress">{{ userAddress }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>

    <!-- 选择区域 -->
    <div class="option" v-show="active">
      <!-- 你的选择区域到底展示什么 , 完全取决于cityList!!! -->
      <span
        @click="selectCity(item)"
        class="ellipsis"
        v-for="item in cityList"
        :key="item.code"
      >
        {{ item.name }}
      </span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
